/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.camera.core.internal.compat.quirk

import androidx.camera.core.impl.QuirkSettings
import androidx.camera.core.impl.QuirkSettingsHolder
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Unit tests for [DeviceQuirks].
 *
 * This test is executed on a physical device instead of a robolectric environment. This is because
 * robolectric's testing modifies the behavior of `DeviceQuirks`, which is loading all quirks for
 * every API call. See src/test/java/androidx/camera/core/internal/compat/quirk/DeviceQuirks.java.
 * This modification makes it impossible to accurately test the reloading of `Quirks` via the
 * `QuirkSettingsHolder.set()` method.
 */
@SmallTest
@RunWith(AndroidJUnit4::class)
class DeviceQuirksTest {

    private val quirkSettingsHolder = QuirkSettingsHolder.instance()

    @After
    fun tearDown() {
        // Reset QuirkSettings after each test to ensure they don't interfere with each other.
        quirkSettingsHolder.reset()
    }

    @Test
    fun initialization_quirksAreUpdatedAndConsistent() {
        val quirks = DeviceQuirks.getAll()
        val quirks2 = DeviceQuirks.getAll()

        assertThat(quirks).isNotNull()
        assertThat(quirks).isSameInstanceAs(quirks2)
    }

    @Test
    fun set_updatesQuirks() {
        val quirks = DeviceQuirks.getAll()
        quirkSettingsHolder.set(QuirkSettings.withAllQuirksDisabled())
        val quirks2 = DeviceQuirks.getAll()

        assertThat(quirks).isNotSameInstanceAs(quirks2)
    }
}
